package com.books.service.comment.impl;

import com.books.mapper.comment.CommentMapper;
import com.books.mapper.common.PeopleMapper;
import com.books.model.entity.comment.Comment;
import com.books.model.entity.common.People;
import com.books.model.tool.CommentTool;
import com.books.service.comment.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 饿熊的咆哮
 * @Date 2021/10/13 16:18
 * @Version 1.0
 */
@Service
@Transactional
public class CommentServiceImpl implements ICommentService {
    @Autowired(required = false)
    private CommentMapper commentMapper;
    @Autowired(required = false)
    private PeopleMapper peopleMapper;
    @Override
    public List<CommentTool> findCommentByBookId(Integer bookId, Integer bookTypeId) {
        List<Comment> comments = commentMapper.findCommentByBookId(bookId,bookTypeId);
        if (comments.size()>0 && comments!=null){
            List<CommentTool> commentTools = new ArrayList<>();
            for (Comment comment : comments) {
                People people = peopleMapper.findByPeopleId(comment.getPeopleId());
                CommentTool commentTool = new CommentTool(comment.getCommentId(),people,comment.getBookId(),comment.getBookTypeId(),comment.getCommentTime(),comment.getCommentStar(),comment.getComment());
                commentTools.add(commentTool);
            }
            return commentTools;
        }else {
            return null;
        }
    }

    @Override
    public void addComment(Comment comment) {
        commentMapper.addComment(comment);
    }

    @Override
    public List<Comment> findCommentByPeopleId(Integer peopleId) {
        return commentMapper.findCommentByPeopleId (peopleId);
    }
}
